**************************************************************
***
***	INTERTEMPORAL EVIDENCE ON THE STRATEGY OF POPULISM IN THE US (Gennaro, Lecce, Morelli)
***	Replication Package
***  
*** Figure 1
***
**************************************************************

clear
est clear 
set gr off


* Indicate location of the replication package here
cd "/Replication Package"


**********************************************
* Upload Data                            *****
**********************************************

use "3_data/for_analysis/congress_election_both.dta", clear



**********************************************
* Variables and sample                   *****
**********************************************

* Sample
keep if uncont==0 & indep==0

* Model
local outvar "pop_dim2_s"
local controls "female i.age_cat_num i.ethnicity2 i.educ_cat len_words i.election_year"


**********************************************
* Run the regressions                    *****
**********************************************


qui reg `outvar' i.outsider_correct##i.competitivedistricts##c.econ `controls' i.state, cluster(district)

qui sum econ
local min = round(r(min), .01)
local max = round(r(max), .01)
local m1 = round(r(min), 1)
local m2 = round(r(max), 1)


**********************************************
* Marginsplots.                          *****
**********************************************


qui margins, at(econ=(`min'(.1)`max') competitive=1) atmeans over(outsider_correct)
marginsplot, recast(line) recastci(rarea) ciopt(color(%20)) ///
	title("Competitive Districts", size(medium)) ///
	xtitle("") ///
	ytitle("Predicted Populism (std)") ///
	graphregion(color(white)) bgcolor(white) ///
	xscale(r(`min' `max')) xlabel(`m1'(1)`m2') ///
	yscale(r(-3 3)) ylabel(-3(1)3) ///
	plot1opts(lpattern(".")) plot2opts(lpattern("--")) ///
	yline(0) legend(off)

gr save temp1, replace
	

qui margins, at(econ=(`min'(.1)`max') competitive=0) atmeans over(outsider_correct)
marginsplot, recast(line) recastci(rarea) ciopt(color(%20)) ///
	title("Non Competitive Districts", size(medium)) ///
	xtitle("") ///
	ytitle("") ///
	graphregion(color(white)) bgcolor(white) ///
	xscale(r(`min' `max')) xlabel(`m1'(1)`m2') ///
	yscale(r(-3 3)) ylabel(-3(1)3) ///
	plot1opts(lpattern(".")) plot2opts(lpattern("--")) ///
	yline(0) ///
 	legend(order(1 "Insider" 2 "Outsider") label(1 "Insider" 2 "Outsider") 	 ///
	cols(1) ring(0) position(5) bmargin(large) symxsize(6) region(lstyle(none))) 
gr save temp2, replace


**********************************************
* Density plots.                         *****
**********************************************


set gr off
qui sum econ 
local min = round(r(min), 1)
local max = round(r(max), 1)
local m1 = round(r(min), 1)
local m2 = round(r(max), 1)
kdensity econ if competitive==1, ///
	fcolor(%15) lc(black%20) w(0.5)	 graphregion(color(white)) ///
	xtitle("Economic Insecurity (std)") ytitle("Density") title("") ///
	note("") ylabel(0(.2).5)
gr save temp3, replace

kdensity econ if competitive==0, ///
	fcolor(%15) lc(black%20) w(0.5)	 graphregion(color(white)) ///
	xtitle("Economic Insecurity (std)") ytitle("") title("") ///
    note("") ylabel(0(.2).5)
gr save temp4, replace


**********************************************
* Combine plots.                         *****
**********************************************


gr combine temp1.gph temp2.gph, r(2) c(2) graphregion(color(white)) ycom xcom ///
	title("")
gr save combo1, replace

gr combine temp3.gph temp4.gph, r(2) c(2) graphregion(color(white)) ycom xcom ///
	title("") fysize(20)
gr save combo2, replace


set gr on 
gr combine combo1.gph combo2.gph, r(2) graphregion(color(white)) 

**********************************************
* Save and cleaning                      *****
**********************************************

* change legend
gr export "4_results/figures/pp_congress_all.png", replace

	
cap erase temp1.gph
cap erase temp2.gph
cap erase temp3.gph 
cap erase temp4.gph 
cap erase combo1.gph 
cap erase combo2.gph
	
